var Cartoon = Class.create();
Object.extend(Cartoon, {	
	add_Chapter:function (){
		new MessageBox(MessageBox.TYPE_OK_CANCEL, $('addChapter').innerHTML , '添加章节',{ok: this.addChapter}, {width:500});
	},
	edit_Chapter:function (id){
		var html = $('panel_'+id).innerHTML;
		new MessageBox(MessageBox.TYPE_OK_CANCEL, html , '修改章节',{ok: this.editChapter.bind(this,id)}, {width:500});
	},
	del_Chapter:function(id){
		new MessageBox(MessageBox.TYPE_OK_CANCEL, '您确定要删除吗？删除会同时删除漫画图片' , '删除章节',{ok: this.delChapter.bind(this,id)}, {width:500});
	},
	/**
	 * 检查是否是拼音
	 */
	checkPY:function(str){
		var patrn=/^[0-9A-Za-z][0-9A-Za-z_-]*$/; 
    	if (!patrn.exec(str)) return false 
    	return true 
	},
	/**
	 * 检查添加项
	 */
	checkSubmit:function(f){
		var myform = $(f);
		 if(!this.checkInput()) return false;
		 if(!this.checkRadio('zone')){
		 	alert('漫画地域 不能为空');
		 	return false;
		 }
		 if(!this.checkRadio('readerGroup')){
		 	alert("读者群不能为空！");
		 	return false;
		 }
		 if(!this.checkRadio('language')){
		 	alert("语言不能为空！");
		 	return false;
		 }
		 if(!this.checkPY($$("input[name='cartoon_py']")[0].value)){
		 	alert("漫画拼错误！");
		 	return false;
		 }
		 if(!this.checkSelect('msel_author2','selected_author')){
		 	alert("请选择作者!");
		 	$('msel_author2').focus();
		 	return false;
		 }else{
		 	
		 }
		 
		 if(!this.checkSelect('msel_cate2','selected_cate')){
		 	alert("请选择分类!");
		 	$('msel_cate2').focus();
		 	return false;
		 }
		 /*if(!this.checkSelect('seselect2','selected_series')){
		 	alert("请选择所属系列!");
		 	$('seselect2').focus();
		 	return false;
		 }*/
		var simple_desc = s_content_editor.getHtml();

		var cartoon_desc = content_editor.getHtml();
		if(!simple_desc || simple_desc.length<=0){
			alert("漫画简介不能为空！");
			s_content_editor.getFocus();
			return false;
		}
		if(!cartoon_desc || cartoon_desc.length<=0){
			alert("漫画详细介绍不能为空！");
			content_editor.getFocus();
			return false;
		}
		
		$('s_desc').value = simple_desc;
		
		$('_cartoon_desc').value = cartoon_desc;

		myform.submit();
	},
	
	checkRadio:function(name){
		var i = 0;
		$$("input[type=radio][name="+name+"]").each(function(e){
			if(e.checked){
				i++;
			}
		});
		if(i>0) return true;
		$$("input[type=radio][name="+name+"]")[0].focus();
		return false;
	},
	
	checkInput:function(){
		$$('input.input_text').each(function(e){
			if(!e.value || e.value.length==0){
				alert(e.title+" 不能为空");
				e.focus();
				throw $continue; 
			}
		});
		return true;
	},
	
	checkSelect:function(sel){
		var mysel = $(sel);
		var val  = '';
		for(var i=0;i<mysel.options.length;i++){
			val +=mysel.options[i].value+",";
		}
		if(!val || val.length==0){
			return false;
		}
		if(arguments.length>=2){
			$(arguments[1]).value = val.substring(0,val.lastIndexOf(','));
		}
		return true;
	},
	addCate:function(){
		var cate_title	=	$('cate_title').value.trim();
		var cate_order	=	$('cate_order').value;
		var cate_recomm	=	1;
		if($("recomm").checked){
			cate_recomm = 2;
		}
		if(!cate_title || cate_title.length<=0 || cate_title.length>60){
			new MessageBox(MessageBox.TYPE_MESSAGE,'分类名必须在1到60个字符之间','警告');
			$$("input[type=text][name=cate_title]")[0].focuce();
			return false;
		}
		var catid = $('cateid').value;
		var url = "/~cartoon.index.addCate";
		new Ajax.Request(url,{
			method:'post',
			parameters: $H({cate_title:cate_title,cate_order:cate_order,cateid:catid,recomm:cate_recomm}),
			onComplete:function(transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					new MessageBox(MessageBox.TYPE_MESSAGE,'分类添加成功','提示',{close:function(){location.reload();}});
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE,'分类添加失败','提示',{close:function(){history.go(-1);}});
					
				}
			}
		})
	},
	addChapter:function(){
		var chapter_title	=	$$("input[type=text][name=chapter_title]")[0].value.trim();
		var chapter_order	=	$$("input[type=text][name=chapter_ordernum]")[0].value.trim();
		var desc = s_content_editor.getHtml();
		if(!chapter_title || chapter_title.length<=0 || chapter_title.length>60){
			new MessageBox(MessageBox.TYPE_MESSAGE,'分类名必须在1到60个字符之间','警告');
			$$("input[type=text][name=chapter_title]")[0].focuce();
			return false;
		}
		var catoon_id = $('_cartoon_id').value;
		var url = "/~cartoon.index.add_chapter";
		if($('btn_addChapter')) $('btn_addChapter').disabled = true;
		new Ajax.Request(url,{
			method:'post',
			parameters: $H({cartoon_id:catoon_id,title:chapter_title,order:chapter_order,desc:desc}),
			onComplete:function(transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					new MessageBox(MessageBox.TYPE_MESSAGE,'章节添加成功','提示',{close:function(){location.reload();}});
					if($('btn_addChapter')) $('btn_addChapter').disabled = false;
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE,'章节添加失败','提示',{close:function(){history.go(-1);}});
				}
			}
		})
	},
	editChapter:function(id){
		var chapter_title	=	$("chapter_title_"+id).value.trim();
		var chapter_order	=	$("order_num_"+id).value.trim();
		var desc = $("info_"+id).value.trim();
		if(!chapter_title || chapter_title.length<=0 || chapter_title.length>60){
			new MessageBox(MessageBox.TYPE_MESSAGE,'分类名必须在1到60个字符之间','警告');
			$$("input[type=text][name=chapter_title]")[0].focuce();
			return false;
		}
		var catoon_id = $('cartoon_id_'+id).value;
		var url = "/~cartoon.index.add_chapter";
		new Ajax.Request(url,{
			method:'post',
			parameters: $H({id:id,cartoon_id:catoon_id,title:chapter_title,order:chapter_order,desc:desc}),
			onComplete:function(transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					new MessageBox(MessageBox.TYPE_MESSAGE,'章节修改成功','提示',{close:function(){location.reload();}});
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE,'章节修改失败','提示',{close:function(){history.go(-1);}});
				}
			}
		});
	},
	delChapter:function(id){
		if(!id){
			new MessageBox(MessageBox.TYPE_MESSAGE,'参数错误','提示',{});
			return false;
		}
		var url = "/~cartoon.index.remove_chapter";
		new Ajax.Request(url,{
			method:'post',
			parameters: $H({id:id}),
			onComplete:function(transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					new MessageBox(MessageBox.TYPE_MESSAGE,'章节删除成功','提示',{close:function(){location.reload();}});
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE,'章节删除失败','提示',{close:function(){history.go(-1);}});
				}
			}
		})
	},
	addAuthor:function(){
		var op = '添加';
		var id = $('authorid').value;
		var name = $$("input[type=text][name=author]")[0].value.trim();
		var nation = $('nation').options[$('nation').selectedIndex].value.trim();
		if(name.length==0 || name.length>60){
			new MessageBox(MessageBox.TYPE_MESSAGE,'作者名称长度不合法','警告');
			return false;
		}	
		if(parseInt(id)>0) op = '修改';
		var url = "/~cartoon.index.add_author";
		new Ajax.Request(url,{
			method:'post',
			parameters: $H({id:id,name:name,nation:nation}),
			onComplete:function(transport){
				var ret = transport.responseText.evalJSON();
				if(ret.result){
					new MessageBox(MessageBox.TYPE_MESSAGE,op+'成功','提示');
					location.reload();
				}else{
					new MessageBox(MessageBox.TYPE_MESSAGE,ret.reson,'提示');
				}
			}
		})
	},
	edit_author:function(id){
		if(!id ){
			new MessageBox(MessageBox.TYPE_MESSAGE,'参数错误','警告');
			return false;
		}
		var title,order,url;
		if(type==1){//添加
			$('cateid').value	=	$('id_'+id).value;
			alert($('id_'+id).value);
			$('cate_title').value = $('title_'+id).value.trim();
			$('cate_order') = $('order_'+id).value.trim();
			url = "/~artoon.index.edit_chapter";
		}else{
			$('cateid').value	=	$('id_'+id).innerHTML;
			$('cate_title').value = $('title_'+id).innerHTML;
			$('cate_order').value = $('order_'+id).innerHTML;
			$('btn_cate').value = "修改";
		}
	},
	edit_cate:function(id,type){
		if(!id ){
			new MessageBox(MessageBox.TYPE_MESSAGE,'参数错误','警告');
			return false;
		}
		var title,order,url;
		if(type==1){//添加
			$('cateid').value	=	$('id_'+id).value;
			alert($('id_'+id).value);
			$('cate_title').value = $('title_'+id).value.trim();
			$('simple_desc').value = $('desc_'+id).value;
			$('cate_order') = $('order_'+id).value.trim();
			url = "/~cartoon.index.edit_chapter";
		}else{
			$('cateid').value	=	$('id_'+id).innerHTML;
			if($('cate_flag_'+id).value == 2){
				$('recomm').checked = true;
			}
			$('cate_title').value = $('title_'+id).innerHTML;
			$('cate_order').value = $('_rank_'+id).innerHTML;
			$('btn_cate').value = "修改";
		}
	},
	setRank:function(id){
		if(!(id )) return false;
		var ranks = parseInt($('_rank_'+id).value);
		if(ranks>0){
			var url = "/~active.active.setRank";
			new Ajax.Request(url,
			{
				method:'post',
				parameters: $H({id:aid,rank:ranks}),
				onComplete:function(transport){
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						return ;
						
					}else{
						new MessageBox(MessageBox.TYPE_MESSAGE,'设置失败','提示');
					}
				}
			}
			);
		}
	},
	
	edit_img:function(id){
		var _img_id 	  = '_img_' + id;
		var _img_span_title = '_img_span_' + id;
		var _img_title  = 'img_title_' + id;
		$(_img_id).hide();
		$(_img_span_title).show();
		$(_img_title).focus();
	},
	
	edit_img_info:function(id){
		var _img_title = $('img_title_'+id).value.stripScripts();
		_img_title = _img_title.trim();		
		var _img_order = $('img_order_'+id).value;		
		var reg = /^[0-9]\d*$/;		
		if(!reg.test(_img_order)){
			new MessageBox(MessageBox.TYPE_MESSAGE, '排序必须为正整数', '', {});
			$('img_order_'+id).focus()
			return false;
		}
		
		if(!_img_title || _img_title.length<=0){
			new MessageBox(MessageBox.TYPE_MESSAGE, '图片名称不能为空', '', {});
			$('img_title_'+id).focus()
			return false;
		}
		if(_img_title.length>50){
			new MessageBox(MessageBox.TYPE_MESSAGE, '图片名称过长，不要超过50个字', '', {});
			$('img_title_'+id).focus()
			return false;
		}
		
		var url = "/~cartoon.index.dit_img_info";
		new Ajax.Request(url,
			{
				method:'post',
				parameters: $H({id:id,title:_img_title,order:_img_order}),
				onComplete:function(transport){
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						new MessageBox(MessageBox.TYPE_MESSAGE,'修改成功！','提示');
						$('_img_span_' + id).hide();
						$('_img_'+id).show();
						
					}else{
						new MessageBox(MessageBox.TYPE_MESSAGE,'设置失败','提示');
					}
				}
			});
	},
	del_img:function(id){
		if(!id){
			new MessageBox(MessageBox.TYPE_MESSAGE, '参数错误', '', {});
			return false;
		}
		new MessageBox(MessageBox.TYPE_YES_NO,'你确定要删除该照片吗','提示',{
			yes:function(){
			var url = "/~cartoon.index.delImg";
			new Ajax.Request(url,
				{
					method:'post',
					parameters: $H({id:id}),
					onComplete:function(transport){
						var ret = transport.responseText.evalJSON();
						if(ret.result){
							new MessageBox(MessageBox.TYPE_MESSAGE,'删除图片成功！','提示');
							location.reload();
						
						}else{
							new MessageBox(MessageBox.TYPE_MESSAGE,'删除图片失败','提示');
						}
					}
				});
		},
		no:function(){return false;}
		});
	},
	chkall:function(chkall){
		 $$("input").each(function(o){if(o.type=="checkbox"){if($(chkall).checked){o.checked=true;} else o.checked=false;}});
	},
	
	getIds:function(chkall){
		var ids = '';
		var split = '';
		$$("input").each(function(nodes){
			if(nodes.type=='checkbox' && nodes.checked){
				if(nodes.id!==chkall){
					ids+=split+nodes.value;
					split = ',';
				}
			}
		}
		);
		return ids;
	},
	recom:function(flag){
		var ids = '';
		if(arguments.lenght>=2){
			ids = arguments[1];
		}else{
			ids = this.getIds('checkall');
		}
		if(!ids){
			new MessageBox(MessageBox.TYPE_MESSAGE,'请选择你要推荐的漫画','提示');
			return false;
		}
		if(!flag) flag = 0;
		var url = '/?_app=cartoon&_controller=index&_action=recommCartoon';
		new Ajax.Request(url,
			{
				method:'post',
				parameters: $H({catoon_id:ids,flag:flag}),
				onComplete:function(transport){
					var ret = transport.responseText.evalJSON();
					if(ret.result){
						new MessageBox(MessageBox.TYPE_MESSAGE,'推荐漫画成功！','提示');
						location.reload();					
					}else{
						new MessageBox(MessageBox.TYPE_MESSAGE,'推荐漫画失败','提示');
					}
				}
			});
	}
});